import AntModal from '@/components/pt/dialog/AntModal'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import { getMessage, addMessage, updateMessage } from '@/api/platform/message'
import { getUserInfoByIds } from '@/api/system/user'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    AntModal,
    SelectUser
  },
  data() {
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加消息表',
      // 表单参数
      form: { type: '0' },
      rules: {
        fromId: [{ required: true, message: '请选择发送用户', trigger: 'blur' }],
        toId: [{ required: true, message: '请选择接收用户', trigger: 'blur' }],
        content: [{ required: true, message: '消息内容不能为空', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fromId: undefined,

        toId: undefined,

        content: undefined,

        type: undefined,

        status: '0'
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加消息信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const messageId = row.id
      getMessage(messageId).then(response => {
        this.form = response.data
        if (this.form.fromId !== null && this.form.toId !== null) {
          getUserInfoByIds({ userIds: `${this.form.fromId};${this.form.toId}` }).then(res => {
            this.form.fromId = {
              ids: this.form.fromId,
              names: res.data.find(item => item.id === this.form.fromId).name
            }
            this.form.toId = {
              ids: this.form.toId,
              names: res.data.find(item => item.id === this.form.toId).name
            }
          })
        }
        this.formTitle = '修改消息表'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.form.fromId !== null && this.form.fromId !== undefined) {
            saveForm.fromId = this.form.fromId.ids
          }
          if (this.form.toId !== null && this.form.toId !== undefined) {
            saveForm.toId = this.form.toId.ids
          }
          if (this.form.id !== undefined) {
            updateMessage(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addMessage(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/message/index'
      this.$router.push(index)
    }
  }
}
